package com.atguigu.lease.web.admin.service.impl;


import com.atguigu.lease.module.entity.*;
import com.atguigu.lease.module.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.RoomInfoMapper;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.atguigu.lease.web.admin.vo.room.RoomSubmitVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo> implements RoomInfoService {
    @Autowired
    private RoomFacilityService roomFacilityService;
    @Autowired
    private RoomLabelService roomLabelService;
    @Autowired
    private RoomLeaseTermService roomLeaseTermService;
    @Autowired
    private RoomPaymentTypeService roomPaymentTypeService;
    @Autowired
    private GraphInfoService graphInfoService;
    @Autowired
    private RoomAttrValueService roomAttrValueService;

    @Override
    public void saveOrUpdateRoom(RoomSubmitVo roomSubmitVo) {
        //1.保存room基本信息
        Long id = roomSubmitVo.getId();
        Boolean updateFlag = id != null && id != 0;
        if (updateFlag) {
            roomFacilityService.remove(new LambdaQueryWrapper<RoomFacility>().eq(RoomFacility::getRoomId, id));
            roomLabelService.remove(new LambdaQueryWrapper<RoomLabel>().eq(RoomLabel::getRoomId, id));
            roomLeaseTermService.remove(new LambdaQueryWrapper<RoomLeaseTerm>().eq(RoomLeaseTerm::getRoomId, id));
            roomPaymentTypeService.remove(new LambdaQueryWrapper<RoomPaymentType>().eq(RoomPaymentType::getRoomId, id));
            graphInfoService.remove(new LambdaQueryWrapper<GraphInfo>().eq(GraphInfo::getItemType, ItemType.ROOM).eq(GraphInfo::getItemId, id));
            roomAttrValueService.remove(new LambdaQueryWrapper<RoomAttrValue>().eq(RoomAttrValue::getRoomId,id));
        }
        super.saveOrUpdate(roomSubmitVo);
        Long id1 = roomSubmitVo.getId();
        roomSubmitVo.getFacilityInfoIds().stream().map(facilityInfoId -> {
            return new RoomFacility(id1,facilityInfoId);
        }).forEach(roomFacilityService::saveOrUpdate);
        roomSubmitVo.getAttrValueIds().stream().map(attrValueId ->{
            return new RoomAttrValue(id1,attrValueId);
        }).forEach(roomAttrValueService::saveOrUpdate);
        roomSubmitVo.getLabelInfoIds().stream().map(labelInfoId->{
            return new RoomLabel(id1,labelInfoId);
        }).forEach(roomLabelService::saveOrUpdate);
        List<GraphInfo>graphInfoList = new ArrayList<>();
        roomSubmitVo.getPaymentTypeIds().stream().map(paymentTypeId ->{
            return new RoomPaymentType(id1,paymentTypeId);
        }).forEach(roomPaymentTypeService::saveOrUpdate);
        roomSubmitVo.getGraphVoList().forEach(graphVo->{
            GraphInfo graphInfo = new GraphInfo();
            BeanUtils.copyProperties(graphVo,graphInfo);
            graphInfo.setItemType(ItemType.ROOM);
            graphInfo.setItemId(id1);
            graphInfo.setName(graphVo.getName());
            graphInfo.setUrl(graphVo.getUrl());
            graphInfoList.add(graphInfo);
        });
        graphInfoService.saveOrUpdateBatch(graphInfoList);


    }
}
